/*
 * Copyright 2008. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the Apache License, Version 2.0. You
 * can find a copy of the license at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */

package com.sinai.mshab.extensions.search;

import java.util.ArrayList;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sinai.mshab.format.ClonableWidget;
import com.sinai.mshab.widgets.ExpandableTable;

/**
 * Presents the user with a widget allowing them to select a category (table
 * name) for which they wish to set up filter rules.
 * 
 * This widget contains an expandable table of {@link SearchField} which specify
 * filtering at column level.
 * 
 * @author serge
 * 
 */
public class SearchCategory extends VerticalPanel implements ClonableWidget {

	private DatabaseStructure structure;
	private ListBox tables;
	private ExpandableTable fields;

	/**
	 * Creates a new search category widget given the databse strcture.
	 * 
	 * @param structure
	 *            the databse structure to represent.
	 */
	public SearchCategory(DatabaseStructure structure) {
		setStyleName("mshab-SearchCategory");
		this.structure = structure;

		// set up the table choice
		tables = new ListBox();
		ArrayList<String> table_names = structure.getTableNames();
		for (int i = 0; i < table_names.size(); i++) {
			tables.addItem(table_names.get(i));
		}
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.setStyleName("mshab-CategoryHeader");
		hpanel.add(new Label("Category: "));
		hpanel.add(tables);
		add(hpanel);

		// should the table change, we need to clear all conditions
		tables.addChangeListener(new TableChangeListener());

		// set up the expandable panel for field names
		fields = new ExpandableTable("Add new condition");
		initFieldsList();
	}

	/**
	 * This will initialize the fields expandable table
	 */
	public void initFieldsList() {
		remove(fields);
		fields = new ExpandableTable("Add new condition");
		fields.includeRemoveLinks();
		fields.add(new SearchField(getStructure(), getSelectedTable()));
		add(fields);
	}

	public String getSelectedTable() {
		return tables.getItemText(tables.getSelectedIndex());
	}

	public DatabaseStructure getStructure() {
		return structure;
	}

	public Widget cloneWidget() {
		return new SearchCategory(structure);
	}

	/**
	 * This will clear the conditions should the table change
	 * 
	 * @author serge
	 * 
	 */
	private class TableChangeListener implements ChangeListener {
		public void onChange(Widget sender) {
			initFieldsList();
		}
	}

	/**
	 * Returns a list of {@link SearchField}s in this category.
	 * 
	 * @return a list of {@link SearchField}s in this category.
	 */
	public ArrayList<SearchField> getConditions() {
		ArrayList<SearchField> conditions = new ArrayList<SearchField>();
		// go through all our rows and throw categories into the arraylist.
		int rows = fields.getRowCount();
		for (int i = 1; i < rows - 1; i++) {
			conditions.add((SearchField) fields.getWidget(i, 0));
		}
		return conditions;
	}
}
